package first.dialogs;

import first.users.BiblioUser;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Locale;
import java.util.ResourceBundle;
/**
 * Bridge between Users and ifferent kinds of Dialogs
 * @author kirill
 *
 */
public abstract class LibDialog 
{
	private BiblioUser User;

	
	public static String findMainPath(String path,Locale l)
	{
		ResourceBundle resb=ResourceBundle.getBundle("first.localiz.SampleRB",l);
		System.out.println(resb.getString("findpath1")+path+"\n"+resb.getString("findpath2")+resb.getString("findpath3"));
		byte b[]=new byte[1];
		byte[] z=new byte[1];
		
		do
		{
			try
			{
				if (System.in.read(b)!=-1)
				{
					if ((char)b[0]=='y') 
					{
						System.out.println(resb.getString("findpath5")+path);
						path=LibDialog.inputCorrectPath(path, l);
						
						return path;
					}
					else
						if ((char)b[0]=='n')
						{	
							System.out.println(resb.getString("InpPath"));
							System.in.read(z);
							InputStreamReader isr= new InputStreamReader(System.in);						
							BufferedReader buf=new BufferedReader(isr);
							path="";
							path=/*(char)z[0]+*/buf.readLine();
							
							System.out.println(resb.getString("findpath5")+path);
							path=LibDialog.inputCorrectPath(path, l);
							
							return path;
						}
				}
			}
			catch(IOException e)
			{
				
			}
			
			if (((char)b[0]!='y')&&((char)b[0]!='n')&&((char)b[0]!='\n')) 
			{
				System.out.println(resb.getString("findpath4")+resb.getString("findpath3"));
			}
		}
		while (((char)b[0]!='y')||((char)b[0]!='n'));
		return null;
	}
	
	public static String inputCorrectPath(String path,Locale l)
	{

		ResourceBundle resb=ResourceBundle.getBundle("first.localiz.SampleRB",l);
		byte[] z=new byte[1];
		boolean b=true;
		do
		{
			//System.out.println("|"+path+"|");
			File f=new File(path);
			b=f.exists()&&f.isDirectory();
			
			if (f.exists()&&f.isDirectory())
			{
				System.out.println(resb.getString("CongrPath"));
				return path;
			}	
			else
			{
				try
				{
					System.out.println(resb.getString("PathNotExists"));
					
					System.out.println(resb.getString("InpPath"));
					System.in.read(z);
					InputStreamReader isr= new InputStreamReader(System.in);
					BufferedReader buf=new BufferedReader(isr);
					path="";
					path=(char)z[0]+buf.readLine();
					//System.out.println(path);
				}
				catch(IOException e)
				{
					
				}
			}
		}
		while (b!=true);
		
		return path;
	}

	public static String inputCorrectFile(String pathh,String path,Locale l)
	{
		ResourceBundle resb=ResourceBundle.getBundle("first.localiz.SampleRB",l);
		byte[] z=new byte[1];
		
		boolean b=true;
		boolean c=false;
		do
		{
			//System.out.println("|"+path+"|");
			File f=new File(pathh+path);
			FileReader rd;
			try {
				rd = new FileReader(f);
				if ((rd.read())!=-1) 
				{
					b=true;
				
				}
				else
				{
					b=false;
				}
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch(IOException e)
			{
				
			}
			
			
			if (b)
			{
				System.out.println(resb.getString("CongrFile"));
				return path;
			}	
			else
			{
				try
				{	
					if (c!=false) System.out.println(resb.getString("FileNotExists"));
					
					System.out.println(resb.getString("InpPath"));
					System.in.read(z);
					InputStreamReader isr= new InputStreamReader(System.in);
					BufferedReader buf=new BufferedReader(isr);
					path="";
					path=(c==false)?buf.readLine():(char)z[0]+buf.readLine();
					c=true;
					System.out.println("|"+pathh+path+"|");
				}
				catch(IOException e)
				{
					
				}
			}
		}
		while (b!=true);
		
		return path;
	}

	
	public static void checkAdmin(String path,Locale l)
	{
		boolean b=false;
		boolean c=false;
		do
		{
			File f=new File(path,"admin.user");
			b=f.exists()&&f.isFile();
			
			if (b)
			{
				
			}
			else
			{
				path=LibDialog.inputCorrectFile(path,"jjh", l);
				c=true;
			}
		}
		while (c!=true);
	}
	
	/**
	 * @param user the user to set
	 */
	public void setUser(BiblioUser user) {
		User = user;
	}

	/**
	 * @return the user
	 */
	public BiblioUser getUser() {
		return User;
	}
}
